import { Toast } from 'vant';

/**
 * @description 分割数字， 每隔三位使用逗号分割一次
 * @param {number}
 * @returns {String} 返回分隔后的字符串
 *
 * @example
 * threeNumberAPointer(33)              // return '33'
 * threeNumberAPointer(1234.56)         // return '1,234.56'
 * threeNumberAPointer(123456789)       // return '123,456,789'
 * threeNumberAPointer(987654.321)      // return '987,654.321'
 * threeNumberAPointer(-987654.3)       // return '-987654.3'
 */
export function threeNumberAPointer(numb) {
  const str = numb.toString();
  const fixedLength = str.split('.')[1] ? str.split('.')[1].length : 0;
  return parseFloat(str)
    .toFixed(fixedLength)
    .replace(/\d(?=(?:\d{3})+\b)/g, `$&,`);
}
/**
 * 返回银行卡号，每隔四个数字添加一个空格
 * @param {String}
 */
export function formatBankCard(str) {
  const newStr = str.replace(/\d(?=(?:\d{4})+\b)/g, `$& `);
  return String(newStr);
}
/**
 * 将数字转换成大写金额
 * 最多只能匹配到千万级别。 只能匹配到小数点后2位。 允许数字和字符串，允许有￥
 * @param {*} Num
 * @example
 * numToChinese(987654); // 玖拾捌万柒仟陆佰伍拾肆元整
 * numToChinese(1214.23); // 壹仟贰佰壹拾肆元贰角叁分
 * numToChinese("456.7"); // 肆佰伍拾陆元柒角
 */
export function numToChinese(Num) {
  if (typeof Num === 'number') {
    Num = toString(Num);
  }
  Num = Num.replace(/,/g, '');
  Num = Num.replace(/ /g, '');
  Num = Num.replace(/￥/g, '');
  if (isNaN(Num)) {
    Toast('请检查小写金额是否正确');
    return '';
  }
  var part = String(Num).split('.');
  var newchar = '';
  for (var i = part[0].length - 1; i >= 0; i--) {
    if (part[0].length > 10) {
      return '';
      // 若数量超过拾亿单位，提示
    }
    var tmpnewchar = '';
    var perchar = part[0].charAt(i);
    switch (perchar) {
      case '0':
        tmpnewchar = '零' + tmpnewchar;
        break;
      case '1':
        tmpnewchar = '壹' + tmpnewchar;
        break;
      case '2':
        tmpnewchar = '贰' + tmpnewchar;
        break;
      case '3':
        tmpnewchar = '叁' + tmpnewchar;
        break;
      case '4':
        tmpnewchar = '肆' + tmpnewchar;
        break;
      case '5':
        tmpnewchar = '伍' + tmpnewchar;
        break;
      case '6':
        tmpnewchar = '陆' + tmpnewchar;
        break;
      case '7':
        tmpnewchar = '柒' + tmpnewchar;
        break;
      case '8':
        tmpnewchar = '捌' + tmpnewchar;
        break;
      case '9':
        tmpnewchar = '玖' + tmpnewchar;
        break;
    }
    switch (part[0].length - i - 1) {
      case 0:
        tmpnewchar = tmpnewchar + '元';
        break;
      case 1:
        if (perchar !== 0) tmpnewchar = tmpnewchar + '拾';
        break;
      case 2:
        if (perchar !== 0) tmpnewchar = tmpnewchar + '佰';
        break;
      case 3:
        if (perchar !== 0) tmpnewchar = tmpnewchar + '仟';
        break;
      case 4:
        tmpnewchar = tmpnewchar + '万';
        break;
      case 5:
        if (perchar !== 0) tmpnewchar = tmpnewchar + '拾';
        break;
      case 6:
        if (perchar !== 0) tmpnewchar = tmpnewchar + '佰';
        break;
      case 7:
        if (perchar !== 0) tmpnewchar = tmpnewchar + '仟';
        break;
      case 8:
        tmpnewchar = tmpnewchar + '亿';
        break;
      case 9:
        tmpnewchar = tmpnewchar + '拾';
        break;
    }
    newchar = tmpnewchar + newchar;
  }
  // 小数点之后进行转化
  if (Num.indexOf('.') !== -1) {
    if (part[1].length > 2) {
      // alert("小数点之后只能保留两位,系统将自动截断");
      part[1] = part[1].substr(0, 2);
    }
    for (i = 0; i < part[1].length; i++) {
      tmpnewchar = '';
      perchar = part[1].charAt(i);
      switch (perchar) {
        case '0':
          tmpnewchar = '零' + tmpnewchar;
          break;
        case '1':
          tmpnewchar = '壹' + tmpnewchar;
          break;
        case '2':
          tmpnewchar = '贰' + tmpnewchar;
          break;
        case '3':
          tmpnewchar = '叁' + tmpnewchar;
          break;
        case '4':
          tmpnewchar = '肆' + tmpnewchar;
          break;
        case '5':
          tmpnewchar = '伍' + tmpnewchar;
          break;
        case '6':
          tmpnewchar = '陆' + tmpnewchar;
          break;
        case '7':
          tmpnewchar = '柒' + tmpnewchar;
          break;
        case '8':
          tmpnewchar = '捌' + tmpnewchar;
          break;
        case '9':
          tmpnewchar = '玖' + tmpnewchar;
          break;
      }
      if (i === 0) tmpnewchar = tmpnewchar + '角';
      if (i === 1) tmpnewchar = tmpnewchar + '分';
      const newchar = newchar + tmpnewchar;
    }
  }
  // 替换所有无用汉字
  while (newchar.search('零零') !== -1) newchar = newchar.replace('零零', '零');
  newchar = newchar.replace('零亿', '亿');
  newchar = newchar.replace('亿万', '亿');
  newchar = newchar.replace('零万', '万');
  newchar = newchar.replace('零元', '元');
  newchar = newchar.replace('零角', '');
  newchar = newchar.replace('零分', '');
  if (newchar.charAt(newchar.length - 1) === '元') {
    newchar = newchar + '整';
  }
  return newchar;
}
